<?php	
	/**
	* @package ParkFind
	* @desc This is the report script. This grabs the user inputted data and submit for review..
	*/
	// Direct calling check
	if (!isset($_POST["details"]))
		die("Do not call this page directly");
		
	session_start();
	
	/**
	* @desc This include file connects to the database.
	*/
	require 'includes/connect.inc';
	
	// Store variables and clean them
	$park_id = mysql_real_escape_string($_GET["id"], $connection);
	$type = mysql_real_escape_string($_POST["type"], $connection);
	$details = mysql_real_escape_string($_POST["details"], $connection);
	$topic = mysql_real_escape_string($_POST["topic"], $connection);
	
	$topic = htmlentities($topic);
	$details = htmlentities($details);	
	
	
	// Make arrays
	$_SESSION["errors"] = array();
	$_SESSION["form"] = array();
	
	// Type validation		
	If ($type == "---")	
		$_SESSION["errors"]["type"] = "<<< Oops";
		
	else	
	{
		$_SESSION["form"]["type"] = $type;
		unset($_SESSION["errors"]["type"]);
	}
	
	// Topic validation		
	If (strlen($topic) == 0)
		$_SESSION["errors"]["topic"] = "Fill in a topic";
		
	else	
	{
		$_SESSION["form"]["topic"] = $topic;
		unset($_SESSION["errors"]["topic"]);
	}
	
	// Details validation		
	If (strlen($details) == 0)	
	
		$_SESSION["errors"]["details"] = "Fill in details";
	
	else	
	{
		$_SESSION["form"]["details"] = $details;
		unset($_SESSION["errors"]["details"]);
	}
		
	// If there were any errors, show the page again
	if (count($_SESSION["errors"]))
	{
		mysql_close($connection); // Close connection
		header("Location: report.php?id=" . $park_id);
		exit;
	}

	/**
	* @desc This include file will load the functions that are required to call 
	*		from the database
	*/
	require 'includes/db_functions.inc';
	
	// Construct report
	$report = "Topic: " . $topic . "\n" .
				"Type: " . $type . "\n\n" .
				$details;
	
	// Add Report
	$result = addReport($_SESSION["user_id"], $park_id, $report);
	
	
	if ($result == TRUE)
		$status = "success";
	else 
		$status = "failed";
	
	// Redirect
	header("Location: reportreceipt.php?result=" . $status);
	
	mysql_close($connection); // Close connection

	
?>
